import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.cjk.CJKAnalyzer;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.analysis.miscellaneous.PerFieldAnalyzerWrapper;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class TestAnalyzer {

    String en = "ho my lady gaga";

    String cn = "迅雷不及掩耳盗铃响叮当仁不让的小马哥教育";


    public void testAnalyzer(Analyzer analyzer,String content) throws Exception {

        //这里按照空格来进行分词
        TokenStream stream = analyzer.tokenStream("content", content);
        stream.reset();
        while (stream.incrementToken()) {
            System.out.println(stream);
        }
    }

    @Test
    public void testStream() throws Exception {
        testAnalyzer(new StandardAnalyzer(), cn);
    }

    /**
     * 中文的分词字典查询
     * @throws Exception
     */
    @Test
    public void testPerFieldAnalyazerWrapper() throws Exception {
        Map<String, Analyzer> fieldAnalyzer = new HashMap<>();
        fieldAnalyzer.put("title", new StandardAnalyzer());
        fieldAnalyzer.put("content", new SimpleAnalyzer());

        PerFieldAnalyzerWrapper wrapper = new PerFieldAnalyzerWrapper(new SimpleAnalyzer(),fieldAnalyzer);
        //这里按照空格来进行分词
        TokenStream stream = wrapper.tokenStream("content", cn);
        stream.reset();
        while (stream.incrementToken()) {
            System.out.println(stream);
        }
    }

    /**
     * 两字分词
     * @throws Exception
     */
    @Test
    public void testCJKAnalyzer() throws Exception {
        testAnalyzer(new CJKAnalyzer(),cn);
    }

    /**
     * 根据字典来分词
     * @throws Exception
     */
    @Test
    public void testSmartChineseAnalyzer() throws Exception {
        testAnalyzer(new SmartChineseAnalyzer(),cn);
    }

    /**
     * 第三方的分词器
     * @throws Exception
     */
    @Test
    public void testIKAnalyzer() throws Exception {
        testAnalyzer(new IKAnalyzer(),cn);
    }

}
